#ifndef STREAM_PREVIEW_WIDGET_H
#define STREAM_PREVIEW_WIDGET_H

#include <QtGui>
#include <opencv/cv.h>
#include <opencv/highgui.h>

#include "def.h"
#include "VideoUpdater.h"
#include "VideoDecoder.h"

class StreamPreviewWidget : public QGroupBox
{
	Q_OBJECT

public:
	StreamPreviewWidget(const int deviceIdx, const int fps, const int vWidth, const int vHeight);
	~StreamPreviewWidget(void);

	void reset(const int deviceIdx, const int fps, const int vWidth, const int vHeight);
	int getCameraResolution(const int deviceIdx, int &width, int &height);

	inline void setPaused(const bool bPaused) { m_bPaused = bPaused; }
	inline void setPlayback(const bool bPlayback) { m_bPlayback = bPlayback; }
	inline const bool isPaused() { return m_bPaused; }
	inline const bool isPlayback() { return m_bPlayback; }
	inline const int videoWidth() { return m_nVideoWidth; }
	inline const int videoHeight() { return m_nVideoHeight; }

	void resetDecoder(PacketList *headers) { m_lpDecoder->reset(headers); }
	void decodePackets(PacketList *packets);

signals:
	void onCaptureVideo(const int width, const int height, const unsigned char *data);
#if DEBUG
	void logged(const char*);
#endif

private:
	QImage *m_imgVideo;
	QLabel *m_lblVideo;

	void createInterface();
	int stopCamera();

	CvCapture *camera;
	void updateImageData();
	void updateImage();
	unsigned char *m_lpRawImageData;

	bool m_bExit;
	VideoUpdater *m_lpUpdater;
	QMutex m_cameraLock;

	int m_nVideoWidth;
	int m_nVideoHeight;
	int m_nFps;
	int m_nQuality;
	bool m_bPaused;
	bool m_bPlayback;
	VideoDecoder *m_lpDecoder;

	friend class VideoUpdater;

private slots:
	void updatePreviewImage();
	void onDecoded(const int width, const int height);
};

#endif // STREAM_PREVIEW_WIDGET_H
